<template>
	<view class="center">
		<view class="center_top">
			<view class="mask"></view>
		</view>
		<view class="center_box_bg">
			<view class="profily">
				<view class="base" @click="handleUser">
					<view class="profily_header" :style="{'background-image': `url(${avatar})`}">

					</view>
					<text>{{userInfo?userInfo.username:'尚未登录'}}</text>
					<image src="../../static/fumou-center-template/setting.png" mode=""></image>
				</view>
				<view class="order_status">
					<view class="status" v-for="item in status">
						<image class="icon" :src="item.url" mode="aspectFill"></image>
						<text>{{item.name}}</text>
					</view>
				</view>
			</view>
			<view class="baiban">

			</view>
			<view class="center_menu">
				<view class="menu_item" v-for="item in menus">
					<image :src="item.icon" mode="aspectFill"></image>
					<text>{{item.name}}</text>
				</view>
			</view>
		</view>
		<view class="padding">
			<button class="cu-btn bg-red block" @click="handleLogout">退出登录</button>
		</view>
	</view>
	
</template>

<script setup>
	import {useStore} from 'vuex'
	import { computed } from "vue";
	import {usePermission} from '../../hooks/usePermission.js'
	import {pathToBase64} from '../../js_sdk/mmmm-image-tools/index.js'
	import AV from 'leancloud-storage'
import { userUpdate } from '../../api/user.js';
	const store = useStore()
	const userInfo = computed(()=>{
		return store.state.user.userInfo
	})
	const avatar = computed(()=>{
		let url = '/static/logo.png' //默认头像
		if(userInfo.value&&userInfo.value.avatar){
			url = userInfo.value.avatar
		}
		return url
	})
	usePermission()
	
	const handleUser = ()=>{
		if(!userInfo.value){
			uni.navigateTo({
				url:'../login/index'
			})
			return
		}
		console.log('头像上传逻辑');
		uni.chooseImage({
			success: async(file) => {
				console.log(file);
				let temp = file.tempFilePaths[0] //图片资源临时路径
				let base64 = await pathToBase64(temp) //将本地资源转换为base64
				// console.log(base64);
				// resume.txt 是文件名
				const cloudFile = new AV.File('uni-img.png', {base64}); //将base64转为leancloud资源对象
				let res = await cloudFile.save()
				// console.log(res);
				let {url} = res.attributes
				userUpdate(userInfo.value,{avatar:url}).then(res=>{
					store.commit('user/initInfoMut',{
						...userInfo.value,
						avatar:url
					})
				})
			}
		})
	}
	
	//退出登录
	const handleLogout = ()=>{
		store.commit('user/initInfoMut',null)
		uni.switchTab({
			url:'/pages/home/home'
		})
	}
	const status = [{
			key: 1,
			name: '待发货',
			url: '../../static/fumou-center-template/one.png'
		},
		{
			key: 2,
			name: '待收货',
			url: '../../static/fumou-center-template/2.png'
		},
		{
			key: 3,
			name: '待评价',
			url: '../../static/fumou-center-template/3.png'
		},
		{
			key: 4,
			name: '全部订单',
			url: '../../static/fumou-center-template/4.png'
		}
	]

	const menus = [{
			name: '我的收藏',
			icon: '../../static/fumou-center-template/5.png',
			key: 1,
		},
		{
			name: '地址管理',
			icon: '../../static/fumou-center-template/6.png',
			key: 2,
		},
		{
			name: '尺码对照表',
			icon: '../../static/fumou-center-template/7.png',
			key: 3,
		},
		{
			name: '帮助中心',
			icon: '../../static/fumou-center-template/8.png',
			key: 4,
		},
		{
			name: '意见反馈',
			icon: '../../static/fumou-center-template/9.png',
			key: 5,
		},
		{
			name: '关于我们',
			icon: '../../static/fumou-center-template/10.png',
			key: 6,
		}

	]
</script>

<style lang="scss">
	page {
		height: 100%;
	}

	.profily,
	.profily_header {
		border-radius: 8px;
	}

	.center {
		height: 100%;

		&_top {
			height: 18%;
			background: url('../../static/fumou-center-template/header.jpg') no-repeat 0% 50%;
			background-size: cover;

			// background: #E6E6E6;
			.mask {
				background: rgba(0, 0, 0, .4);
				height: 100%;
			}
		}

		&_box_bg {
			background: #F9F9F9;
			position: relative;

			.profily {
				position: absolute;
				width: 90%;
				// border:1px solid #F7F7F7;
				margin: 0 auto;
				top: -100upx;
				left: 5%;
				background: #FEFEFE;
				padding: 35upx;
				box-sizing: border-box;
				box-shadow: 0px 2px 5px #EDEDED;
			}
		}
	}

	.base {
		display: flex;
		align-items: center;
		border-bottom: 2px solid #F6F6F6;
		padding-bottom: 35upx;
		position: relative;

		.profily_header {
			height: 120upx;
			width: 120upx;
			background-image: url('../../static/fumou-center-template/header.jpg');
			background-size: 100%;
		}

		text {
			margin-left: 20px;
			font-size: 30upx;
		}

		image {
			position: absolute;
			height: 40upx;
			width: 40upx;
			right: 0px;
			top: 0px;
		}
	}

	.order_status {
		display: flex;
		justify-content: space-between;
		margin-top: 35upx;

		.status {
			width: 140upx;
			font-size: 24upx;
			text-align: center;
			letter-spacing: .5px;
			display: flex;
			flex-direction: column;

			.icon {
				width: 50upx;
				height: 50upx;
				margin: 0 auto;
				margin-bottom: 5px;

			}
		}
	}

	.baiban {
		background: #FEFEFE;
		height: 300upx;
	}

	.center_menu {
		width: 100%;
		display: inline-block;

		.menu_item {
			font-size: 28upx;
			letter-spacing: 1px;
			padding: 14px 5%;
			background: #FEFEFE;
			overflow: hidden;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			position: relative;
			border-bottom: 1px solid #EFEFEF;

			&:hover {
				background: #F6F6F6 !important;
			}

			&::after {
				content: '';
				width: 30upx;
				height: 30upx;
				position: absolute;
				right: 5%;
				background: url('../../static/fumou-center-template/right.png') no-repeat;
				background-size: 100%;
			}

			text:nth-of-type(1) {
				margin-left: 10px;
			}

			image {
				width: 40upx;
				height: 40upx;
			}

			&:nth-of-type(4) {
				margin-top: 10px;
			}
		}
	}
</style>
